/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DelPersonPanel.java
 *
 * Created on 2011-12-5, 17:25:59
 */
package mscs.server.admin.view;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import mscs.data.pojo.Goods;
import mscs.data.pojo.Person;
import mscs.server.admin.logic.DeletePersonUtil;
import mscs.server.admin.logic.IDeletePersonUtil;
import mscs.server.admin.logic.ISearchPersonUtil;
import mscs.server.admin.logic.SearchPersonUtil;
import mscs.server.view.MscsServerView;

/**
 *
 * @author Administrator
 */
public class DelPersonPanel extends javax.swing.JPanel {
    private DefaultTableModel tableModel = new DefaultTableModel();
    private IDeletePersonUtil delutil= new DeletePersonUtil();
    private ISearchPersonUtil searchutil = new SearchPersonUtil();
    private List<Person> searchList = new ArrayList<Person>();
    MscsServerView serverView;

    /** Creates new form DelPersonPanel */
    public DelPersonPanel(MscsServerView parent) {
        this.serverView = parent;
        initComponents();
        refreshTable();
    }
    
    public void refreshTable(){
        tableModel.setDataVector(TableModelFactory.toListDataVector(this.searchList),
                TableModelFactory.PERSON_COLUMN_NAMES);
        this.buttonSubmit.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        buttonSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonSubmit = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mscs.server.view.MscsServer.class).getContext().getResourceMap(DelPersonPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        buttonSearch.setText(resourceMap.getString("buttonSearch.text")); // NOI18N
        buttonSearch.setName("buttonSearch"); // NOI18N
        buttonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSearchActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(tableModel);
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        buttonSubmit.setText(resourceMap.getString("buttonSubmit.text")); // NOI18N
        buttonSubmit.setName("buttonSubmit"); // NOI18N
        buttonSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSubmitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonSearch))
                    .addComponent(buttonSubmit, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonSubmit)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void buttonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSearchActionPerformed
    this.searchResponse();
}//GEN-LAST:event_buttonSearchActionPerformed

private void buttonSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSubmitActionPerformed
    if(JOptionPane.showConfirmDialog(this, "此操作不可还原，确认要删除吗")==JOptionPane.OK_OPTION){
        this.delutil.remove(this.searchList.get(0));
    }
    this.searchList = new ArrayList<Person>();
    refreshTable();
}//GEN-LAST:event_buttonSubmitActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
    this.searchResponse();
}//GEN-LAST:event_jTextField1ActionPerformed

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
    this.buttonSubmit.setEnabled(true);
}//GEN-LAST:event_jTable1MouseClicked

private void searchResponse(){
    if(this.jTextField1.getText().equals("")){
        this.searchList = this.searchutil.getAll();
    }else{
        this.searchList = this.searchutil.searchByID(this.jTextField1.getText());
    }
    refreshTable();
    if(this.searchList.isEmpty()){
        JOptionPane.showMessageDialog(this, "无此员工");
    }else{
        this.buttonSubmit.setEnabled(true);
    }
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSearch;
    private javax.swing.JButton buttonSubmit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
